package com.snatch.snatchnd.core;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.snatch.common.constant.SnatchConstant;
import com.snatch.common.vo.NodeTaskInput;
import com.snatch.common.vo.RatioDataInput;
import com.snatch.snatchnd.service.impl.NodeIntegration;
import com.snatch.snatchnd.system.AccountData;
import com.snatch.work.service.SnatchControl;
import com.snatch.work.service.SnatchService;
import com.snatch.work.vo.SnatchDataInput;
import com.snatch.work.vo.ThirdInfo;

@Component
@Scope("prototype")
public class SnatchThread extends Thread {

	protected Logger logger = Logger.getLogger(SnatchThread.class);

	NodeTaskInput nodeTaskInput;

	@Autowired
	private NodeIntegration nodeIntegration;

	/**
	 * 版本号
	 */
	private Long version=0L;

	private boolean runFlag = true;// 抓取线程运行和销毁的信号量
	
	private long ASSISTA_SLEEP=10*1000;
	
	private int nodataCount = 0;
	
	private SnatchService hgSnatchDataImpl;
	
	@Override
	public void run() {
		while (runFlag) {
			RatioDataInput ratioDataInput = new RatioDataInput();
			ThirdInfo thirdInfo = null;
			try {
				long time = System.currentTimeMillis();
				// logger.info("获取帐号开始!");
				thirdInfo = AccountData.findThirdInfo(nodeTaskInput
						.getWebCode());
				if (thirdInfo != null && runFlag) {
					nodeTaskInput.setMaintenType(0);
					thirdInfo.setSnatch(true);
					// logger.info("采集开始！");
					SnatchDataInput snatchDataInput = hgSnatchDataImpl.getData(
							nodeTaskInput, thirdInfo);
					if(!thirdInfo.isValid()){
						ratioDataInput.setError(thirdInfo.getWhyValid());
					}else if(nodeTaskInput.getMaintenType() !=0){
						ratioDataInput.setError(SnatchConstant.ERROR_THIRD_MAINTEN);
						//如果内页维护就只采集1面数据  
						nodeTaskInput.setPage(1);
					}else if (snatchDataInput == null || snatchDataInput.getDatas() == null || snatchDataInput.getDatas().length == 0) {
						ratioDataInput.setError(SnatchConstant.ERROR_THIRD_NODATA);
					} else {
						if (runFlag) {
							// logger.info("分析数据开始：");
							
							ratioDataInput = hgSnatchDataImpl
									.gatherData(snatchDataInput);
							if(null == ratioDataInput.getSportinfos() ||  ratioDataInput.getSportinfos().size() ==0){
								nodataCount++;
								ratioDataInput.setError(SnatchConstant.ERROR_THIRD_NOSPORT);
							}else{
								nodataCount = 0;
								nodeTaskInput.setPage(ratioDataInput.getSumPage());
							}
						}
					}
					ratioDataInput.setSnatchCost(System.currentTimeMillis() - time);
					ratioDataInput.setVersion(version);
					
					if (runFlag) {
						ratioDataInput.setOnlyCode(nodeTaskInput.getOnlyCode());
						ratioDataInput.setUserName(snatchDataInput.getUserName());
						ratioDataInput.setTaskCode(snatchDataInput.getNodeTaskInput().getOnlyCode());
						ratioDataInput.setRatioType(snatchDataInput.getNodeTaskInput().getRatioType());
						ratioDataInput.setWebCode(snatchDataInput.getNodeTaskInput().getWebCode());
						ratioDataInput.setGameCode(snatchDataInput.getNodeTaskInput().getGameCode());
						ratioDataInput.setItemName(snatchDataInput.getNodeTaskInput().getItemName());
						ratioDataInput.setItemType(snatchDataInput.getNodeTaskInput().getItemType());
						
						//logger.info("发数据开始：");
						ratioDataInput.setSystemTime(System.currentTimeMillis());
						version = nodeIntegration
								.ratioDataInput(ratioDataInput);
						/*logger.info("发数据开始时间："
								+ ratioDataInput.getOnlyCode() + "  "
								+ (System.currentTimeMillis() - time));*/
					}
					if (thirdInfo != null) {
						thirdInfo.setSnatch(false);
					}
				}
				// logger.info(nodeTaskInput.getTaskCode()+" 版本号： "+ratioDataInput.getVersion()+" 采集总结束："+(System.currentTimeMillis()-time));
				Thread.sleep(getsleepTime());
			} catch (Exception e) {
				logger.error("采集任务错误", e);
				ratioDataInput.setError("采集报错！");
			} finally {
				if (thirdInfo != null) {
					if (thirdInfo.isValid()) {
						thirdInfo.setSnatch(false);
					} else {
						AccountData.logBack(thirdInfo);
					}
				}
			}
		}
		logger.info("采集任务结束" + nodeTaskInput);

	}
	
	
	public Long getsleepTime(){
		boolean addtime = false;
		if(nodeTaskInput.getMaintenType() !=0){
			addtime = true;
		}
		if(nodataCount>5){
			addtime = true;
		}
		if(addtime)
			return nodeTaskInput.getSleepTiem()+ASSISTA_SLEEP;
		return nodeTaskInput.getSleepTiem();
	}
	
	public void init(NodeTaskInput nodeTaskInput){
		this.nodeTaskInput = nodeTaskInput;
		hgSnatchDataImpl = SnatchControl.getSnatchService(nodeTaskInput.getWebCode());
	}
	

}
